<template>
  <div>
    <div
      v-if="isShowError === 0"
      v-loading="loading"
      class="login-container"
      element-loading-text="授权中..."
    />
    <ErrorBox v-if="isShowError === 1" :error_description="error_description" />
    <SuccessBox v-if="isShowError === 2" :jump_url.sync="jump_url" />
    <SuccessBox2 v-if="isShowError === 3" />
  </div>
</template>
<script>
import { auth } from "@/api/auth";
import ErrorBox from "./components/ErrorBox";
import SuccessBox from "./components/SuccessBox";
import SuccessBox2 from "./components/SuccessBox2";
export default {
  name: "Authorization",
  components: { ErrorBox, SuccessBox, SuccessBox2 },
  data() {
    return {
      query: {},
      loading: true,
      code: "",
      itemId: "",
      isShowError: 0,
      error_description: "",
      jump_url: "",
      authCode: "",
      state: "",
    };
  },
  created() {
    if (
      this.$route.query &&
      this.$route.query.authCode &&
      this.$route.query.state
    ) {
      this.authCode = this.$route.query.authCode || "";
      this.state = this.$route.query.state || "";
    }
  },
  mounted() {
    if (this.authCode && this.state) {
      this.handleAuth();
    }
  },
  destroyed() {},
  methods: {
    async handleAuth() {
      const { authCode, state } = this;
      const obj = { authCode, state };
      try {
        const res = await auth(obj);
        console.log('####### handleAuth-res', res)
        this.loading = false;
        if (res.success) {
          this.jump_url = res.data.redirect;
          this.isShowError = 2;
        } else {
          this.error_description = res.errorMessage;
          this.isShowError = 1;
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
